// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/testing/expression/fuzzer_toolkit.h>

#include <gtest/gtest.h>
#include <pollux/common/file/temp_file_path.h>
#include <pollux/testing/vector/vector_test_base.h>

namespace kumo::pollux::fuzzer::test {
class FuzzerToolKitTest : public testing::Test,
                          public kumo::pollux::VectorBuilder {
 protected:
  static void SetUpTestCase() {
    memory::MemoryManager::testingSetInstance({});
  }

  bool compareBuffers(const BufferPtr& lhs, const BufferPtr& rhs) {
    if (!lhs && !rhs) {
      return true;
    }
    if ((lhs && !rhs) || (!lhs && rhs) || lhs->size() != rhs->size()) {
      return false;
    }
    return memcmp(lhs->as<char>(), rhs->as<char>(), lhs->size()) == 0;
  }

  bool equals(const InputRowMetadata& lhs, const InputRowMetadata& rhs) {
    return lhs.columnsToWrapInLazy == rhs.columnsToWrapInLazy &&
        lhs.columnsToWrapInCommonDictionary ==
        rhs.columnsToWrapInCommonDictionary;
  }
};

TEST_F(FuzzerToolKitTest, inputRowMetadataRoundTrip) {
  InputRowMetadata metadata;
  metadata.columnsToWrapInLazy = {1, -2, 3, -4, 5};
  metadata.columnsToWrapInCommonDictionary = {1, 2, 3, 4, 5};

  {
    auto path = exec::test::TempFilePath::create();
    metadata.saveToFile(path->getPath().c_str());
    auto copy =
        InputRowMetadata::restoreFromFile(path->getPath().c_str(), pool());
    ASSERT_TRUE(equals(metadata, copy));
  }
}
} // namespace kumo::pollux::fuzzer::test
